package com.zx.ymy.base

import android.os.Bundle
import android.view.MotionEvent
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.blankj.utilcode.util.ScreenUtils
import com.example.basecomponent.TipDialog
import com.uber.autodispose.AutoDispose
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider
import com.zx.yixun.util.MyUtils
import com.zx.yixun.util.RxSchedulers
import com.zx.ymy.net.NetObserver
import io.reactivex.Observable
import java.util.concurrent.TimeUnit
import com.gyf.immersionbar.ImmersionBar


abstract class BaseActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        onContentCreateBefore()
        setContentView(getContentId())
        getToolbar()?.setNavigationOnClickListener {
            onBackPressed()
        }
        screenWidth = ScreenUtils.getScreenWidth()
        screenHeight = ScreenUtils.getScreenHeight()
        onContentCreate(savedInstanceState)
    }

    abstract fun getToolbar(): Toolbar?

    abstract fun getContentId(): Int

    open fun onContentCreateBefore() {

    }

    abstract fun onContentCreate(savedInstanceState: Bundle?)

    var screenWidth = 720
    var screenHeight = 1920
    private var progressDialog: TipDialog? = null
    private var successDialog: TipDialog? = null
    private var errorDialog: TipDialog? = null
    private var normalDialog:TipDialog? = null

    fun showProgress(tipWord: String? = null) {
        if (progressDialog == null) {
            progressDialog = TipDialog.Builder(this)
                    .setIconType(TipDialog.Builder.ICON_TYPE_LOADING)
                    .setTipWord(tipWord)
                    .create()
        } else {
            progressDialog?.update(tipWord)
        }
        //if (progressDialog?.isShowing!!) return
        progressDialog?.show()
    }

    fun showSuccess(tipWord: String? = "", delay: Long = Constant.DelayTime) {
        if (successDialog == null) {
            successDialog = TipDialog.Builder(this)
                    .setIconType(TipDialog.Builder.ICON_TYPE_SUCCESS)
                    .setTipWord(tipWord)
                    .create()
        } else {
            successDialog?.update(tipWord)
        }
        if (successDialog?.isShowing!!) return
        successDialog?.show()
        Observable.timer(delay, TimeUnit.MILLISECONDS)
                .subscribeOn(RxSchedulers.io)
                .observeOn(RxSchedulers.ui)
                .`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
                .subscribe {
                    successDialog?.dismiss()
                }
    }

    fun showError(tipWord: String? = "", delay: Long = Constant.DelayTime) {
        if (errorDialog == null) {
            errorDialog = TipDialog.Builder(this)
                    .setIconType(TipDialog.Builder.ICON_TYPE_NOTHING)
                    .setTipWord(tipWord)
                    .create()
        } else {
            errorDialog?.update(tipWord)
        }
        if (errorDialog?.isShowing!!) return
        errorDialog?.show()
        Observable.timer(delay, TimeUnit.MILLISECONDS)
                .subscribeOn(RxSchedulers.io)
                .observeOn(RxSchedulers.ui)
                .`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
                .subscribe {
                    errorDialog?.dismiss()
                }
    }

    fun showNormal(tipWord: String? = "", delay: Long = Constant.DelayTime) {
        if (normalDialog == null) {
            normalDialog = TipDialog.Builder(this)
                .setIconType(TipDialog.Builder.ICON_TYPE_NOTHING)
                .setTipWord(tipWord)
                .create()
        } else {
            normalDialog?.update(tipWord)
        }
        if (normalDialog?.isShowing!!) return
        normalDialog?.show()
        Observable.timer(delay, TimeUnit.MILLISECONDS)
            .subscribeOn(RxSchedulers.io)
            .observeOn(RxSchedulers.ui)
            .`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
            .subscribe {
                normalDialog?.dismiss()
            }
    }

    fun hidProgress() {
        progressDialog?.dismiss()
    }

    override fun onStop() {
        super.onStop()
        hidProgress()
        errorDialog?.dismiss()
        normalDialog?.dismiss()
        successDialog?.dismiss()
    }


    protected fun <T> Observable<BaseResponse<T>>.runRx(onSuccess: ((res: T?) -> Unit)?,
                                                        onFailure: ((errorMsg: String?) -> Unit)? = null) {
        this.subscribeOn(RxSchedulers.getScheduler())
                .observeOn(RxSchedulers.ui)
                .`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle)))
                .subscribe(object : NetObserver<T>(null, onSuccess, onFailure) {
                    override fun onFailure(e: Throwable, code: Int?, errorMsg: String?) {
                        super.onFailure(e, code, errorMsg)
                        showError(errorMsg)
                    }
                })
    }

    protected fun <T> Observable<BaseResponse<T>>.runRxLoading(onSuccess: ((res: T?) -> Unit)?,
                                                               onFailure: ((errorMsg: String?) -> Unit)? = null) {
        this.subscribeOn(RxSchedulers.getScheduler())
                .observeOn(RxSchedulers.ui)
                .`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle)))
                .subscribe(object : NetObserver<T>(this@BaseActivity, onSuccess, onFailure) {
                    override fun onFailure(e: Throwable, code: Int?, errorMsg: String?) {
                        super.onFailure(e, code, errorMsg)
                        showError(errorMsg)
                    }
                })
    }

    override fun moveTaskToBack(nonRoot: Boolean): Boolean {
        return super.moveTaskToBack(true)
    }

    fun ImmersionBar(color: Int) {
        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(color)
                .autoStatusBarDarkModeEnable(true, 0.2f) //自动状态栏字体变色，必须指定状态栏颜色才可以自动变色哦
                .init()
    }

    //防止用户短时间重复点击
    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (ev?.action == MotionEvent.ACTION_DOWN){
            // 判断连续点击事件时间差
            if (MyUtils.isFastClick()){
                return true;
            }
        }
        return super.dispatchTouchEvent(ev)
    }

}



























